/**
 * @项目：my
 * @创建人：ifuner
 * @创建人邮箱：zhoupengfei@kaiheikeji.com
 * @创建时间：2019-07-30 13:59:50
 * */

import store from "../../store";
import create from "../../utils/create";

const config = require("../../config");
// console.log("config", config)
// const app = getApp()
let _clickNum = 0;
create(store, {
  data: {
    version: "",
    mineInfo: store.data.mineInfo || {},
    hasLogin: false,
    defaultUserImg: "",
    mtDoorStatus: false,
    feedBackStatus: wx.canIUse("button.open-type.feedback")
  },

  onLoad: function(target) {
    this.update();
    this.initVersion();
    // this.store.loadFont();
  },
  onShow() {
    const { mineInfo, hasLogin } = this.store.data;
    !Object.keys(mineInfo).length && hasLogin && wx.startPullDownRefresh();
  },
  onHide() {
    _clickNum = 0;
  },
  onPullDownRefresh() {
    const stopPull = () => {
      wx.stopPullDownRefresh();
    };
    if (!this.store.data.hasLogin) {
      stopPull();
      return false;
    }
    wx.vibrateShort();
    this.getData().then(() => {
      console.log("刷新成功");
      stopPull();
    }).catch(() => {
      stopPull();
    });
  },
  onLogin(target) {
    /*
    * 1. 在这个事件里面调用初始化业务
    * 2. 登录了就调接口 没登录就清楚之前的信息
    * */
    this.store.isLogin() ? this.getData() : this.update({
      "mineInfo": {}
    });
  },
  handleLogin() {
    this.selectComponent("#login").showLoginNow();
  },
  initVersion() {
    console.log("config", config);
    if (config.env === "prod") {
      this.setData({
        version: config.version
      });
    } else {
      this.setData({
        version: `${config.env} - ${config.version}`
      });
    }
  },
  copyId() {
    const uid = this.data.mineInfo.user_info.chicken_id;
    uid && this.wxApi("setClipboardData", {
      data: uid
    }).then(() => {
      this.$Message({
        content: "复制成功",
        type: "success"
      });
    });
  },
  updateImg(target) {
    if (target.detail.errMsg.indexOf("ok") > 0) {
      let avatar = target.detail.userInfo.avatarUrl;
      if (!avatar) {
        this.$Message({
          content: "微信头像为空",
          type: "error"
        });
        return false;
      }
      this.$http.python.post("PY_UPDATE_AVATAR", { data: { avatar } }).then(res => {
        wx.startPullDownRefresh();
      }).catch(error => {
        this.$Message({
          content: error.msg || "修改失败",
          type: "error"
        });
      });
    }
  },
  previewImage() {
    const imgUrl = Object.keys(this.data.mineInfo).length && this.data.mineInfo.user_info.avatar;
    imgUrl && this.wxApi("previewImage", { urls: [imgUrl] });
  },
  getData() {
    return new Promise((resolve, reject) => {
      this.store.getMyUserInfo().then(res => {
        this.update();
        resolve();
      }).catch(err => {
        reject();
      });
    });
  },
  openDoor() {
    _clickNum++;
    if (_clickNum >= 15) {
      this.selectComponent("#mtDoor").showModal();
      this.setData({
        mtDoorStatus: true
      });
      _clickNum = 0;
    }
    _clickNum > 6 && this.setData({
      mtDoorStatus: true
    });
  }
});
